/**  
 * @Title: UserServiceImpl.java
 * @Package com.xzit.tmms.service.user
 * @Description: TODO
 * @author 董亮亮
 * @date 2016年6月23日 上午8:36:47
 * @version V1.0  
 */
package com.xzit.tmms.service.user.impl;

import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.xzit.tmms.bean.user.Right;
import com.xzit.tmms.bean.user.Role;
import com.xzit.tmms.bean.user.User;
import com.xzit.tmms.common.exception.ServiceException;
import com.xzit.tmms.common.page.Page;
import com.xzit.tmms.common.util.CommonUtil;
import com.xzit.tmms.mapper.user.RightMapper;
import com.xzit.tmms.mapper.user.RoleMapper;
import com.xzit.tmms.mapper.user.UserMapper;
import com.xzit.tmms.service.user.UserService;

/**
 * @ClassName: UserServiceImpl
 * @Description: TODO
 * @author 董亮亮
 * @date 2016年6月23日 上午8:36:47
 */
@Service("userService")
public class UserServiceImpl implements UserService {

	@Resource
	private UserMapper userMapper;

	@Resource
	private RoleMapper roleMapper;

	@Resource
	private RightMapper rightMapper;

	@Override
	public Page<Map<String, Object>> queryUser(Page<Map<String, Object>> page) {
		userMapper.queryUser(page);
		return page;
	}

	@Override
	public void addUser(User user) throws ServiceException {
		try {
			userMapper.save(user);
		} catch (Exception e) {
			throw new ServiceException(e);
		}
	}

	@Override
	public void deleteUser(Integer userId) throws ServiceException {
		try {
			userMapper.delete(userId);
		} catch (Exception e) {
			throw new ServiceException(e);
		}
	}

	@Override
	public User getUserById(Integer userId) throws ServiceException {
		try {
			return userMapper.getById(userId);
		} catch (Exception e) {
			throw new ServiceException(e);
		}
	}

	@Override
	public void updateUser(User user) throws ServiceException {
		try {
			userMapper.update(user);
		} catch (Exception e) {
			throw new ServiceException("更新用户失败");
		}
	}

	@Override
	public Map<String, Object> getUserByAccount(String account) throws ServiceException {
		Map<String, Object> user = null;
		try {
			// 参数校验
			if (CommonUtil.isNotEmpty(account)) {
				user = userMapper.getUserByAccount(account);
				// 计算权限
				// 权限计算
				if (CommonUtil.isNotEmpty(user)) {
					// 获取角色的全部权限
					Integer roleId = Integer.parseInt(user.get("roleId").toString());
					// 角色不为空
					if (CommonUtil.isNotEmpty(roleId)) {
						// 超级管理员
						Role role = roleMapper.getById(roleId);
						if (role != null && role.getRoleValue().equals("admin")) {
							user.put("superAdmin", "1");
							return user;
						}
						// 获取角色的所有权限
						List<Right> rights = roleMapper.getRightsByRole(roleId);
						if (CommonUtil.isNotEmpty(rights)) {
							// 获取最高权限位
							Integer maxPos = Integer.parseInt(rightMapper.selectTopPosCode().get("topPos").toString());
							long[] rightSum = new long[maxPos + 1];
							// 计算总和
							long code = 0;
							int pos = 0;
							for (Right right : rights) {
								pos = right.getRightPos();
								code = right.getRightCode();
								rightSum[pos] |= code;
							}
							System.out.println(rightSum);
							// 将权限总和放入session
							user.put("rights", rightSum);
							// 释放资源
							rights = null;
						}
					}
				}
			}
		} catch (Exception e) {
			throw new ServiceException("查询用户异常");
		}
		return user;
	}

}
